package cn.nutz.controller;

import cn.nutz.entity.User;
import cn.nutz.service.impl.UserServiceImpl;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Jianghao on 2017/8/27
 *
 * @howechiang
 */
@RestController
@RequestMapping("/user")
public class UserController extends Controller {

    private static Logger log = LoggerFactory.getLogger(UserController.class);

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public User save(@RequestBody User user) {

        userService.insert(user);
        return user;
    }

    @RequestMapping("/all")
    public List<User> sort() {

        List<User> people = userService.findAll();
        return people;
    }

    @RequestMapping("/page")
    public QueryResult page(@RequestParam("pageNo") int pageNumber, @RequestParam("pageSize") int pageSize) {
        Pager pager = dao.createPager(pageNumber, pageSize);
        pager.setRecordCount(dao.count(User.class));
        return new QueryResult(dao.query(User.class, null), pager);
    }
}
